package com.cskaoyan.gateway.controller.shopping;

import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.shopping.IHomepageService;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.dto.*;
import com.mall.user.annotation.Anoymous;
import com.mall.user.constants.SysRetCodeConstants;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

/**
 * @program: project4-mall
 * @time: 2021/10/31
 * @author: yangshuai
 **/

/**
 * 主页显示
 */

@RestController
@RequestMapping("/shopping")
public class HomepageController {

    @Reference(timeout = 3000,retries = 0,check = false)
    IHomepageService iHomepageService;


    /**
     * 主页显示
     * @return
     */
    @GetMapping("/homepage")
    @Anoymous
    public ResponseData homepage(){

        // 调用service方法
        HomePageResponse homepage = iHomepageService.homepage();

        // 成功
        if (ShoppingRetCode.SUCCESS.getCode().equals(homepage.getCode())){
            return new ResponseUtil().setData(homepage.getPanelContentItemDtos());
        }
        // 失败
        return new ResponseUtil().setErrorMsg(homepage.getMsg());
    }

    /**
     * 导航栏显示
     * @return
     */
    @GetMapping("/navigation")
    @Anoymous
    public ResponseData navigation(){

        // 调用service方法
        NavListResponse response = iHomepageService.selectNavigation();
        // 成功
        if (ShoppingRetCode.SUCCESS.getCode().equals(response.getCode())){
            return new ResponseUtil().setData(response.getPannelContentDtos());
        }
        // 失败
        return new ResponseUtil().setErrorMsg(response.getCode());
    }

    /**
     * 列举所有商品种类
     * @return
     */
    @GetMapping("/categories")
    @Anoymous
    public ResponseData categories(){

        // 执行service方法
        AllProductCateResponse response = iHomepageService.selectAllProductCate();
        // 成功
        if (ShoppingRetCode.SUCCESS.getCode().equals(response.getCode())){
            return new ResponseUtil().setData(response.getProductCateDtoList());
        }
        // 失败
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }

    /**
     *  主页搜索实现
     *
     */
    @PostMapping("search")
    @Anoymous
    public ResponseData searchProduct(@RequestBody SearchProductRequest searchProductRequest){

        // 调用服务
        SearchProductResponse searchProductResponse = iHomepageService.searchProduct(searchProductRequest);
        // 根据返回的code值判断是否成功
        if(searchProductResponse.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())){
            // 搜索成功
            return new ResponseUtil<>().setData(searchProductResponse);
        }
        // 搜索失败
        return new ResponseUtil<>().setErrorMsg(searchProductResponse.getMsg());
    }

    /**
     *  主页搜索get请求实现
     *
     */
    @GetMapping("search/{key}")
    @Anoymous
    public ResponseData reachProductGet(@PathVariable String key){
        SearchProductRequest searchProductRequest = new SearchProductRequest();
        searchProductRequest.setKey(key);
        // 调用服务
        SearchProductResponse searchProductResponse = iHomepageService.searchProduct(searchProductRequest);
        System.out.println("getResponse:"+ searchProductResponse);
        // 根据返回的code值判断是否成功
        if(searchProductResponse.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())){
            // 搜索成功
            return new ResponseUtil<>().setData(searchProductResponse);
        }
        // 搜索失败
        return new ResponseUtil<>().setErrorMsg(searchProductResponse.getMsg());
    }

}
